import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../city_information.dart';
import 'state.dart';

class ApplyDetailLogic extends GetxController {
  final state = ApplyDetailState();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    state.id = Get.arguments['id'].toString();
    print('订单id');
    print(state.id);
    state.status = Get.arguments['status'] ?? 1;
    state.ordertype = Get.arguments['ordertype'] ?? 1;
    var cityParams = {
      "access_token": SpUtil.getString('token'),
    };
    Api.cityInformation(cityParams).listen((event) {
      // print('城市信息  event = $event');
      if (event['code'] == 0) {
        AddressInformation addressInformation = AddressInformation.fromJson(event);
        Map<String, dynamic> mapData = addressInformation.toJson();
        state.addressList = mapData['children'];
      }
      update();
    });
    keFu();
    showBuild();

    /// 获取订单详情
    var params = {
      'access_token': SpUtil.getString('token'),
      'parent_tml_num_id': state.id,
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };

    Api.tkDetail(params).listen((event) {
      state.detail = event;

      state.prv_num_id = event['return_prv_num_id'];
      state.city_num_id = event['return_city_num_id'];
      state.city_area_num_id = event['return_city_area_num_id'];

      // state.provinceId = event['return_prv_num_id'];
      // state.cityId = event['return_city_num_id'];
      // state.areaId = event['return_city_area_num_id'];
      // state.townId = event['return_town_num_id'];

      state.return_prv_name = event['return_prv_name'];
      state.return_city_name = event['return_city_name'];
      state.return_city_area_name = event['return_city_area_name'];

      // state.provinceName = event['return_prv_name'];
      // state.cityName = event['return_city_name'];
      // state.areaName = event['return_city_area_name'];
      // state.townName = event['return_town_name'];

      state.value = event['tran_sim_name'];
      state.controller.text = event['return_adress'];
      state.controller1.text = event['return_name'];
      state.controller2.text = event['return_mobile'];
      state.controller3.text = event['shiptranno'];
      if (event['tran_sim_name'] == '自送') {
        state.type = 2;
      } else {
        state.type = 1;
      }
      if (event['evidence_pic'] == '') {
        state.img = [];
      } else {
        List list = event['evidence_pic'].split(",");
        state.img = list;
      }
      update();
    });

    var params1 = {
      'tml_num_id': state.id,
      'tml_type': 1,
    };

    Api.tkjd(params1).listen((event) {
      state.time = event['operate_his'][0]['create_dtme'];
      update();
    });
  }

  // 获取客服信息
  void keFu() {
    print('获取客服信息 ======== 获取客服信息11111111');
    Api.MaterialGoodsSelect1({"config_name": "platform_ec_shop_num_id"}).listen((event) {
      print('获取客服信息 ======== 获取客服信息 ********  event = $event');
      var configValue = event["config_value"];
      Api.getAccessID(configValue.toString()).listen((event) {
        print('获取客服信息 ======== 获取客服信息  event = $event');
        // state.accessID = event["access_id"];
        state.keFuString = event['access_id'];
        update();
      });
    });
  }

  void submit() {
    /// 获取订单详情
    var params = {
      'access_token': SpUtil.getString('token'),
      'tml_operation_type': 3,
      'tml_num_id': state.id.toString(),
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };

    print(params);
    Api.cxtk(params).listen((event) {
      if (event['code'] == 0) {
        toast('撤销成功');
        var params = {
          'access_token': SpUtil.getString('token'),
          'parent_tml_num_id': state.id,
          'usr_num_id': SpUtil.getInt('empeNumId'),
        };

        Api.tkDetail(params).listen((event) {
          state.detail = event;

          state.prv_num_id = event['return_prv_num_id'];
          state.city_num_id = event['return_city_num_id'];
          state.city_area_num_id = event['return_city_area_num_id'];

          // state.provinceId = event['return_prv_num_id'];
          // state.cityId = event['return_city_num_id'];
          // state.areaId = event['return_city_area_num_id'];
          // state.townId = event['return_town_num_id'];

          state.return_prv_name = event['return_prv_name'];
          state.return_city_name = event['return_city_name'];
          state.return_city_area_name = event['return_city_area_name'];

          // state.provinceName = event['return_prv_name'];
          // state.cityName = event['return_city_name'];
          // state.areaName = event['return_city_area_name'];
          // state.townName = event['return_town_name'];

          state.value = event['tran_sim_name'];
          state.controller.text = event['return_adress'];
          state.controller1.text = event['return_name'];
          state.controller2.text = event['return_mobile'];
          state.controller3.text = event['shiptranno'];
          if (event['tran_sim_name'] == '自送') {
            state.type = 2;
          } else {
            state.type = 1;
          }
          if (event['evidence_pic'] == '') {
            state.img = [];
          } else {
            List list = event['evidence_pic'].split(",");
            state.img = list;
          }
          update();
        });
      } else {
        toast(event['message']);
      }
      update();
    });
  }

  /// 快递公司
  void showBuild() {
    var params = {
      'access_token': SpUtil.getString('token'),
      'sql_id': 'POS-0049',
    };
    Api.kdgs(params).listen((event) {
      if (event['code'] == 0) {
        List list = event['results'];
        list.forEach((element) {
          element['selected'] = false;
        });
        state.kdList = list;
      }
      update();
    });
  }

  void select(int i) {
    List list = state.kdList;
    list.forEach((element) {
      element['selected'] = false;
    });
    state.kdList[i]['selected'] = true;
    update();
  }

  void submitKd() {
    List list = state.kdList;
    var data = null;
    list.forEach((element) {
      if (element['selected'] == true) {
        data = element;
      }
    });
    state.value = data['value'];
    state.code = int.parse(data['code']);
    update();
  }

  void setType(int index) {
    state.type = index;
    update();
  }

  /// 选择地址
  void selectCity(value) {
    print('选择地址');
    print(value);
    state.prv_num_id = int.parse(value.provinceCode);
    state.city_num_id = int.parse(value.cityCode);
    state.city_area_num_id = int.parse(value.areaCode);
    state.return_prv_name = value.province;
    state.return_city_name = value.city;
    state.return_city_area_name = value.area;
    update();
  }
  // void selectCity(int index, Map checkedItem, List backAddressItemList) {
  //   switch (index) {
  //     case 0:
  //       state.province = checkedItem;
  //       state.provinceList = backAddressItemList;

  //       state.city = {};
  //       state.cityList = [];
  //       state.district = {};
  //       state.cityAreaList = [];
  //       state.street = {};
  //       state.townList = [];

  //       break;
  //     case 1:
  //       state.city = checkedItem;
  //       state.cityList = backAddressItemList;

  //       state.district = {};
  //       state.cityAreaList = [];
  //       state.street = {};
  //       state.townList = [];

  //       break;
  //     case 2:
  //       state.district = checkedItem;
  //       state.cityAreaList = backAddressItemList;

  //       state.street = {};
  //       state.townList = [];

  //       break;
  //     case 3:
  //       state.street = checkedItem;
  //       state.townList = backAddressItemList;
  //       break;
  //   }

  //   if (index == 3) {
  //     state.provinceName = state.province["name"];
  //     state.cityName = state.city["name"];
  //     state.areaName = state.district["name"];
  //     state.townName = state.street["name"];
  //     state.provinceId = int.parse(state.province["id"]);
  //     state.cityId = int.parse(state.city["id"]);
  //     state.areaId = int.parse(state.district["id"]);
  //     state.townId = int.parse(state.street["id"]);

  //     update();
  //   }
  // }

  /// 提交资料
  void submitReturn() {
    var params;
    if (state.type == 1) {
      params = {
        'tml_num_id': state.id, // 退款订单号
        'express_code': state.code, // 快递公司编号
        'ship_no': state.controller3.text, // 物流单号
        'prv_num_id': state.prv_num_id, // 省编码
        'city_num_id': state.city_num_id, // 市编码
        'city_area_num_id': state.city_area_num_id, // 区编码

        // 'prv_num_id': state.provinceId, // 省编码
        // 'city_num_id': state.cityId, // 市编码
        // 'city_area_num_id': state.areaId, // 区编码
        // 'town_num_id': state.townId, // 镇编码
        'detail_adr': state.controller.text, // 详细地址
        'cont_empe': state.controller1.text, // 收货人
        'cont_empe_telephone': state.controller2.text, // 收货人电话
      };
    } else {
      params = {
        'tml_num_id': state.id, // 退款订单号
        'express_code': -1, // 快递公司编号
        'prv_num_id': state.prv_num_id, // 省编码
        'city_num_id': state.city_num_id, // 市编码
        'city_area_num_id': state.city_area_num_id, // 区编码

        // 'prv_num_id': state.provinceId, // 省编码
        // 'city_num_id': state.cityId, // 市编码
        // 'city_area_num_id': state.areaId, // 区编码
        // 'town_num_id': state.townId, // 镇编码
        'detail_adr': state.controller.text, // 详细地址
        'cont_empe': state.controller1.text, // 收货人
        'cont_empe_telephone': state.controller2.text, // 收货人电话
      };
    }
    Api.returnProduct(params).listen((event) {
      if (event['code'] == 0) {
        toast('提交成功');
        var params = {
          'access_token': SpUtil.getString('token'),
          'parent_tml_num_id': state.id,
          'usr_num_id': SpUtil.getInt('empeNumId'),
        };

        Api.tkDetail(params).listen((event) {
          state.detail = event;
      
          state.prv_num_id = event['return_prv_num_id'];
          state.city_num_id = event['return_city_num_id'];
          state.city_area_num_id = event['return_city_area_num_id'];

          // state.provinceId = event['return_prv_num_id'];
          // state.cityId = event['return_city_num_id'];
          // state.areaId = event['return_city_area_num_id'];
          // state.townId = event['return_town_num_id'];
          
          state.return_prv_name = event['return_prv_name'];
          state.return_city_name = event['return_city_name'];
          state.return_city_area_name = event['return_city_area_name'];

          // state.provinceName = event['return_prv_name'];
          // state.cityName = event['return_city_name'];
          // state.areaName = event['return_city_area_name'];
          // state.townName = event['return_town_name'];

          state.value = event['tran_sim_name'];
          state.controller.text = event['return_adress'];
          state.controller1.text = event['return_name'];
          state.controller2.text = event['return_mobile'];
          state.controller3.text = event['shiptranno'];
          if (event['tran_sim_name'] == '自送') {
            state.type = 2;
          } else {
            state.type = 1;
          }
          if (event['evidence_pic'] == '') {
            state.img = [];
          } else {
            List list = event['evidence_pic'].split(",");
            state.img = list;
          }
          update();
        });
      } else {
        toast(event['message']);
      }
      update();
    });
  }
}
